#ifndef NODE_EDITOR_SUBS_H
#define NODE_EDITOR_SUBS_H

#include <QWidget>
#include <QTableWidget>

#include "./line_mdle/line_mdle.h"
#include "../public_opt_subs/table_mdle/operate_blck/QTableWidgetOperate.h"
#include "./../public_opt_subs/table_mdle/funciton_blck/QTableWidgetSave.h"
#include <QSettings>
#include "./device_mdle/device_select.h"
#include "../MACRO.h"
// --------------------------------------------
// 增加设备列表
//加入到新建逻辑
#include "./device_mdle/device_mdle.h"
#include "./device_mdle/device_file_browser.h"
#include "./device_mdle/device_file_cache.h"

#define DEVICE_LIST_SET \
    case 0: dev_new = new device_mdle; break; \
    case 1: dev_new = new device_file_browser; break; \
    case 2: dev_new = new device_file_cache; break; \
    default: dev_new = new device_mdle;  break; \

//加入到选项卡
#define DEVICE_LIST_STRING \
    { \
    "device_mdle", \
    "device_file_browser", \
    "device_file_cache" \
    } \

namespace Ui {
class node_editor_subs;
}

class node_editor_subs : public QWidget
{
    Q_OBJECT

public:
    explicit node_editor_subs(QString dir = "", QWidget *parent = nullptr);
    ~node_editor_subs();

private slots:
//    void on_ui_btn_add_clicked();
    void on_table_double_click(int row, int column);
    void on_table_click(int row, int column);
    void on_menu_move_click(int row, int column);
    void on_menu_connect_click(int row, int column);
    void on_menu_copy_click(int row, int column);
    void on_menu_create_click(int row, int column);
    void on_menu_delete_click(int row, int column);

    void on_dev_update(device_mdle * dev);
    void on_devs_update();
    void on_lin_update(line_mdle * dev);
    void on_lins_update();
private:
    bool pointInDevice(int x, int y,device_mdle * dev); //! 判断点是否在器件范围
    bool pointInDevices(int x, int y); //! 判断点是否在器件范围
    bool tableIsEmpty(int x, int y); //! 判断表格点是否为空白画布
    void clearFailLine(); //! 删除所有的非法连线
    void connectLineLogic(QMetaObject::Connection & conn_out_tx,
                          QMetaObject::Connection & conn_out_rx,
                          int out_index,int in_index); //连接输入和输出
    void clearAllConnect(); //! 清除所有的连接
private:
    Ui::node_editor_subs *ui;
    // --------------------------------------------
    // 增加表格用于模拟画布
    QTableWidgetOperate * table; //! 画布表格
    QAbstractItemModel * model; //! 设置模型
    // --------------------------------------------
    // 增加说明表格
    QTableWidgetSave * table_help; //! 增加帮助文档


    // --------------------------------------------
    // 增加画布的动作
    bool move_flag; //移动状态
    bool connect_flag; //连接器件
    bool copy_flag; //复制器件
    // --------------------------------------------
    // 颜色区分
    // 定义颜色定义,以便后续提供统一的修改
    const QColor color_dev = Qt::blue;
    const QColor color_info = Qt::yellow;
    const QColor color_io  = Qt::magenta;
    const QColor color_lin = Qt::green;
    const QColor color_sel = Qt::cyan;
    // --------------------------------------------
    // 创建器件
private:
//    device_mdle *dev_demo; //创建测试器件
//    device_mdle *dev_demo2; //创建测试器件

    QList <device_mdle *>dev_list; //创建器件
    device_mdle * dev_sel; //选中的器件
    QList <line_mdle *>lin_list; //创建线队列
    line_mdle * lin_sel; //选中的线
    device_mdle * dev_point; //点对应的器件
    device_mdle * dev_new; //新建的器件
    device_mdle * dev_connect_out; //连接的输出器件
    device_mdle * dev_connect_in ; //连接的输入器件
    QVector<QMetaObject::Connection> connections_list; //线的逻辑连接记录
    line_mdle * lin_new; //选中的线
    bool lin_inout_error; //连线输出输入类型错误
    //器件创建引导页面
    const QStringList select_list = DEVICE_LIST_STRING; //导入可以创建的器件
    device_select * u_device_select; //获取选择的器件
    // --------------------------------------------
    // 器件缓存
public slots:
    void save(); //缓存画布为文件
    void load(); //读取缓存的文件
private:
    //缓存的工作目录
    QSettings *settings;
    QString work_dir; //工作目录
    void load_dir(); //加载目录

};

#endif // NODE_EDITOR_SUBS_H
